var gulp = require('gulp');
var path = require('path');
var ts = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var merge = require('merge2');
var del = require('del');
var tsProject = ts.createProject('tsconfig.json');
var uglify = require('gulp-uglify');
var rename = require("gulp-rename");
var less = require('gulp-less');
var LessAutoprefix = require('less-plugin-autoprefix');
var autoprefix = new LessAutoprefix({ browsers: ['last 2 versions'] });
var minifyCss = require('gulp-clean-css');

gulp.task('clean', function() {
    return del('dist/**/*');
});

gulp.task('build', ['build-sripts-uglify', 'build-styles-minify']);

gulp.task('build-styles', function () {
    return gulp.src('less/carousellite.less')
        .pipe(sourcemaps.init())
        .pipe(less({
            paths: [path.join(__dirname, 'node_modules')],
            plugins: [autoprefix]
        }))
        .pipe(sourcemaps.write('.', {sourceRoot: '../src'}))
        .pipe(gulp.dest('dist'));
});
gulp.task('build-styles-minify', ['build-styles'], function() {
    return gulp.src('dist/carousellite.css')
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(minifyCss())
        .pipe(rename('carousellite.min.css'))
        .pipe(sourcemaps.write('.', {includeContent: false}))
        .pipe(gulp.dest('dist'));
});

gulp.task('build-sripts', function () {
    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())
        .pipe(tsProject());

    return merge([
        tsResult.dts.pipe(gulp.dest('dist')),
        tsResult.js
            .pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: '../src'}))
            .pipe(gulp.dest('dist'))
    ]);
});
gulp.task('build-sripts-uglify', ['build-sripts'], function() {
    return gulp.src('dist/carousellite.js')
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(uglify())
        .pipe(rename('carousellite.min.js'))
        .pipe(sourcemaps.write('.', {includeContent: false}))
        .pipe(gulp.dest('dist'));
});